<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button id="btn">发送</button>
  <script>
    // 创建socket对象
    const socket = new WebSocket('ws://localhost:8082')
    // 绑定一个连接的监听
    socket.onopen = () => {
      console.log('连接成功!')
    }
    // 绑定接收服务器端消息的监听
    socket.onmessage = (event) => {
      console.log('接收到消息', event.data)
      const status = JSON.parse(event.data).status
      if (status==='已支付') {
        alert('已支付, 跳转到支付成功的界面')
      }
    }

    document.getElementById('btn').onclick = () => {
      // 向服务器发消息
      socket.send('abc')
    }
  </script>
</body>
</html>